
function mySetInterval(callback, delay, n = Infinity) {
    let count = 0;
    let timer;
    timer = setTimeout(function run() {
        callback();
        if (count >= n) {
            clearTimeout(timer);
            return; // 注意，出口
        }
        // console.log(count, timer);
        count++;
        timer = setTimeout(run, delay);
    }, delay);
}

// case

setInterval(() => {
    console.log('1')
}, 1000)

mySetInterval(() => {
    console.log('2')
}, 1000, 3)